'''
Created on 07/04/2011

@author: Eran_Z

IMDB functions, based on Alex's implementations
'''

import sys

try:
    import imdb
except ImportError:
    print 'You need to install the IMDbPY package!'
    sys.exit(1)

#a handler/gateway for all IMDB API functions
imdbObj = imdb.IMDb()


def getLinkToMovie(title):
    try:
        # Do the search, and get the results (a list of Movie objects).
        results = imdbObj.search_movie(title)
    except imdb.IMDbError, e:
        print "Probably you're not connected to Internet.  Complete error report:"
        print e
        sys.exit(3)
    
    
    for movie in results:
        #print '%s\t: %s : %s' % (movie.movieID, imdbObj.get_imdbID(movie),
        #                            movie['long imdb title'])
        imdbURL = imdbObj.get_imdbURL(movie)
        if imdbURL:
            #print 'IMDb URL: %s' % imdbURL
            return imdbURL

def getMovieSummary(title):
    try:
        # Do the search, and get the results (a list of Movie objects).
        results = imdbObj.search_movie(title)
    except imdb.IMDbError, e:
        print "Probably you're not connected to Internet.  Complete error report:"
        print e
        sys.exit(3)
    
    
    return results[0].summary()

def getMovieYear(title):
    try:
        # Do the search, and get the results (a list of Movie objects).
        results = imdbObj.search_movie(title)
    except imdb.IMDbError, e:
        print "Probably you're not connected to Internet.  Complete error report:"
        print e
        sys.exit(3)
    
    
    return results[0].get('year')

def getLinkAndYear(title):
    try:
        # Do the search, and get the results (a list of Movie objects).
        results = imdbObj.search_movie(title)
    except imdb.IMDbError, e:
        print "Probably you're not connected to Internet.  Complete error report:"
        print e
        sys.exit(3)
    
    return (imdbObj.get_imdbURL(results[0]), results[0].get('year'))


"""
# example usage of imdb API:

def searchMovie(title):
    try:
        # Do the search, and get the results (a list of Movie objects).
        results = imdbObj.search_movie(title)
    except imdb.IMDbError, e:
        print "Probably you're not connected to Internet.  Complete error report:"
        print e
        sys.exit(3)
    
    # Print the results.
    print '    %s result%s for "%s":' % (len(results),
                                        ('', 's')[len(results) != 1],
                                        title)
    print 'movieID\t: imdbID : title'
    
    # Print the long imdb title for every movie.
    
    
    for movie in results:
        print '%s\t: %s : %s' % (movie.movieID, imdbObj.get_imdbID(movie),
                                    movie['long imdb title'])
    ####################################################################################    
    #if we want to check if movie with exact such title exist in the system
    print "here all movies that have exact such title as 'title':"
    for movie in results:
        if (movie['title']==title):
            print '%s\t: %s : %s' % (movie.movieID, imdbObj.get_imdbID(movie),
                                       movie['long imdb title'])


def searchPerson(name):
    try:
        # Do the search, and get the results (a list of Person objects).
        results = imdbObj.search_person(name)
    except imdb.IMDbError, e:
        print "Probably you're not connected to Internet.  Complete error report:"
        print e
        sys.exit(3)
    
    # Print the results.
    print '    %s result%s for "%s":' % (len(results),
                                        ('', 's')[len(results) != 1],
                                        name)
    print 'personID\t: imdbID : name'
    
    # Print the long imdb name for every person.
    for person in results:
        print '%s\t: %s : %s' % (person.personID, imdbObj.get_imdbID(person),
                                    person['long imdb name'])



def topNMovies(n):
    top250 = imdbObj.get_top250_movies()
    
    ml = top250[:n]
    print ''
    print 'top 10 movies'
    print 'rating\tvotes\ttitle'
    for movie in ml:
        print '%s\t%s\t%s' % (movie.get('rating'), movie.get('votes'),
                                    movie['long imdb title'])

"""
